#include <stdio.h>
#include "SEGGER_RTT.h"

#ifdef __GNUC__
/* With GCC, small printf (option LD Linker->Libraries->Small printf
   set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(char ch)
#define GETCHAR_PROTOTYPE int __io_getchar()
#else
#define PUTCHAR_PROTOTYPE int fputc(char ch)
#define GETCHAR_PROTOTYPE int fgetc()
#endif /* __GNUC__ */

PUTCHAR_PROTOTYPE {
    if (ch == '\n') {
        SEGGER_RTT_PutCharSkip(0, '\r');
    }
    SEGGER_RTT_PutCharSkip(0, ch);
    return ch;
}

GETCHAR_PROTOTYPE {
    uint8_t ch = 0;
    SEGGER_RTT_ReadNoLock(0, &ch, 1);
    return ch;
}